CPRE := @
ifeq ($(V),1)
CPRE :=
VERB := --verbose
endif

MK_GENERATED_IMGS_PATH:=generated

.PHONY:startup
startup: all

all:
	@echo "Build Solution by $(BOARD) "
	$(CPRE) scons $(VERB) --board=$(BOARD) -j4
	@echo YoC SDK Done

	@echo [INFO] Create bin files
	$(CPRE) product image $(MK_GENERATED_IMGS_PATH)/images.zip -i $(MK_GENERATED_IMGS_PATH)/data -l -v "v1.0" -p
	$(CPRE) product image $(MK_GENERATED_IMGS_PATH)/images.zip -e $(MK_GENERATED_IMGS_PATH) -x
	$(CPRE) product image $(MK_GENERATED_IMGS_PATH)/images.zip -e $(MK_GENERATED_IMGS_PATH)/data -b

	@echo [INFO] Create wm bin files
	$(CPRE) csky-elfabiv2-objcopy -O binary ./out/offline_audio_w800/yoc.elf ./out/offline_audio_w800/yoc.bin
	$(CPRE) gcc ../../components/chip_w800/wmsdk/tools/w800/wm_tool.c -Wall -lpthread -O2 -o ./out/offline_audio_w800/wm_tool
	$(CPRE) ./out/offline_audio_w800/wm_tool -b ./out/offline_audio_w800/yoc.bin -fc 0 -it 1 -ih 81FA000 -ra 8012000 -ua 8132000 -nh 0 -un 0 -vs yoc-7.3.0
	$(CPRE) cat ../../components/chip_w800/wmsdk/tools/w800/w800_secboot.img ./out/offline_audio_w800/yoc.img > ./out/offline_audio_w800/yoc.fls
	$(CPRE) ./out/offline_audio_w800/wm_tool -b ./out/offline_audio_w800/yoc.img -fc 1 -it 1 -ih 81FA000 -ra 8012000 -ua 8132000 -nh 0 -un 0 -vs yoc-7.3.0
	$(CPRE) mv ./out/offline_audio_w800/yoc_gz.img ./out/offline_audio_w800/yoc_ota.bin
	@echo [INFO] Create fls file
	$(CPRE) cp $(MK_GENERATED_IMGS_PATH)/data/boot boot.bin
	$(CPRE) cp $(MK_GENERATED_IMGS_PATH)/data/prim prim.bin
	$(CPRE) cp $(MK_GENERATED_IMGS_PATH)/data/imtb imtb.bin
	$(CPRE) ./out/offline_audio_w800/wm_tool -b boot.bin -fc 0 -it 512 -ih 8002000 -ra 8002400 -ua 8132000 -nh 81FA000 -un 0
	$(CPRE) ./out/offline_audio_w800/wm_tool -b prim.bin -fc 0 -it 1 -ih 81FA000 -ra 8012000 -ua 8132000 -nh 0 -un 0
	$(CPRE) ./out/offline_audio_w800/wm_tool -b imtb.bin -fc 0 -it 1 -ih 20047400 -ra 8010000 -ua 0 -nh 0 -un 0
	$(CPRE) cat boot.img prim.img imtb.img > ./out/offline_audio_w800/yoc_total.fls
	$(CPRE) rm -f boot.bin prim.bin imtb.bin boot.img prim.img imtb.img

run:all
	$(CPRE) ./out/offline_audio_w800/wm_tool -c $(DL_PORT) -rs at -ds 2000000 -dl ./out/offline_audio_w800/yoc.fls -sl str -ws 115200

.PHONY:flash
flash:
	$(CPRE) product flash $(MK_GENERATED_IMGS_PATH)/images.zip -w prim -f script/w800_flash.elf

.PHONY:flashdata
flashdata:
	$(CPRE) product flashbin ir_data.bin 0x8201000 -f script/w800_spiflash.elf

.PHONY:flashall
flashall:
	$(CPRE) product flash $(MK_GENERATED_IMGS_PATH)/images.zip -a -f script/w800_flash.elf

sdk:
	$(CPRE) yoc sdk

.PHONY:clean
clean:
	$(CPRE) scons -c
	$(CPRE) find . -name "*.[od]" -delete
	$(CPRE) rm yoc_sdk yoc.* generated out -rf
